package br.com.serjaum.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.com.serjaum.modelo.Matricula;



public class MatriculaDAO extends DAO<Matricula> {


	private Logger logger = Logger.getLogger(MatriculaDAO.class);

	public MatriculaDAO(Session session, Class<?> classe) {
		super(session, classe);
	}
	

	public Matricula buscaMatriculaById(Long idMatricula) {
		logger.info("buscaMatriculaById : " + idMatricula);
		return (Matricula) session.load(Matricula.class, idMatricula);
	}
	
	@SuppressWarnings("unchecked")
	public List<Matricula> buscaMatriculaByCodigoCurso(String codigo) {
		
		logger.info("buscaMatriculaByCodigoCurso : " + codigo);
		
		Criteria c = session.createCriteria(Matricula.class);
		
		c.add(Restrictions.ilike("codigoCursoMatricula", "%" + codigo + "%"));
		c.addOrder(Order.asc("codigoCursoMatricula"));

		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Matricula> buscaMatriculaByDni(String dni) {
		
		logger.info("buscaMatriculaByCodigoCurso : " + dni);
		
		Criteria c = session.createCriteria(Matricula.class);
		c.add(Restrictions.ilike("dniMatricula", "%" + dni + "%"));
		c.addOrder(Order.asc("dniMatricula"));

		return c.list();
	}

	public Matricula buscaMatriculaByDniCodigoCurso(String dni, String codigo) {
		
		logger.info("buscaMatriculaByDniCodigoCurso : " + "dni->" + dni + " " + "codigoCurso->" + codigo);
		
		Criteria c = session.createCriteria(Matricula.class);
		if (dni!=null)
		c.add(Restrictions.ilike("dniMatricula", "%" + dni + "%"));
		if(codigo!=null)
		c.add(Restrictions.ilike("codigoCursoMatricula", "%" + codigo + "%"));	
		//c.addOrder(Order.asc("dniMatricula"));
	
		
		
		return (Matricula)c.uniqueResult();
	}
	

}
